package com.bishe.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bishe.demo.mapper.CommodityMapper;
import com.bishe.demo.mapper.SaleMapper;
import com.bishe.demo.pojo.Commodity;
import com.bishe.demo.pojo.Sale;
import com.bishe.demo.pojo.vo.Status;
import com.bishe.demo.utils.Judge;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class SaleService {

    @Autowired
    SaleMapper saleMapper;
    @Autowired
    CommodityMapper commodityMapper;


    public Status addSale(Integer vipId,String commodityName, Integer commodityNumber, String commodityCategory){
//     Status status = new Status();
//     if (StringUtils.isBlank(commodityName)
//             ||StringUtils.isBlank(Integer.toString(commodityNumber))
//             ||StringUtils.isBlank(commodityCategory)
//             ||StringUtils.isBlank(Integer.toString(commodityCost))
//             ||StringUtils.isBlank(Integer.toString(commodityPrice))){
//         status.setMsg("订单信息不能为空！");
//         status.setCode(false);
//         return status;
//     }
//     if (commodityCost<=0){
//         status.setMsg("请输入合法进价！");
//         status.setCode(false);
//         return status;
//     }
//        if (commodityPrice<=0){
//            status.setMsg("请输入合法售价！");
//            status.setCode(false);
//            return status;
//        }
//        if (commodityNumber<=0){
//            status.setMsg("请输入合法数量！");
//            status.setCode(false);
//            return status;
//        }
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("commodity_name",commodityName);
        Integer commodityCost = commodityMapper.selectOne(queryWrapper).getCommodityCost();
        Integer  commodityPrice = commodityMapper.selectOne(queryWrapper).getCommodityPrice();
        Status status = Judge.judge(commodityName, commodityNumber, commodityCategory,commodityCost, commodityPrice);

        if (vipId==null){
            status.setMsg("没有会员编号可添加！");
            status.setCode(false);
            return status;
        }

        if (!status.getMsg().equals("成功")){
            System.out.println(status.getMsg());
            System.out.println("我执行了——————————");
            return status;
        }
        System.out.println("sale中的"+status.getMsg());



        //添加之前要判断，商品库里面有没有该商品，数量够不够
        HashMap<String,Object> map = new HashMap<>();
        map.put("commodity_name",commodityName);
        List<Commodity> commodityList = commodityMapper.selectByMap(map);

        if (!commodityList.get(0).getCommodityCategory().equals(commodityCategory)){
            status.setMsg("商品类型不匹配！");
            status.setCode(false);
            return status;
        }
        if (commodityNumber>commodityList.get(0).getCommodityNumber()){
            status.setMsg("商品栏的该商品数量不足！");
            status.setCode(false);
            return status;
        }


        //如果数量够了
        //1.刚刚好够，直接插入，然后删除商品栏中商品
        System.out.println(commodityName+"---------的"+commodityNumber);
        System.out.println(commodityList.get(0).getCommodityName()+"---------的"+commodityList.get(0).getCommodityNumber());
        System.out.println(commodityNumber==commodityList.get(0).getCommodityNumber());
        if (commodityNumber.equals(commodityList.get(0).getCommodityNumber())){
            //删除
            System.out.println("我执行了嘛——————————————————");
            commodityMapper.deleteByMap(map);

            Sale sale = new Sale();
            sale.setCommodityCost(commodityCost);
            sale.setCommodityCategory(commodityCategory);
            sale.setCommodityName(commodityName);
            sale.setCommodityNumber(commodityNumber);
            sale.setCommodityPrice(commodityPrice);
            sale.setVipId(vipId);

            saleMapper.insert(sale);

        }
        //如果数量够，则减少数量，然后插入
        if (commodityNumber<commodityList.get(0).getCommodityNumber()){
            Commodity commodity =new Commodity();
            commodity.setCommodityId(commodityList.get(0).getCommodityId());
            commodity.setCommodityNumber(commodityList.get(0).getCommodityNumber()-commodityNumber);

            commodityMapper.updateById(commodity);
            Sale sale = new Sale();
            sale.setCommodityCost(commodityCost);
            sale.setCommodityCategory(commodityCategory);
            sale.setCommodityName(commodityName);
            sale.setCommodityNumber(commodityNumber);
            sale.setCommodityPrice(commodityPrice);
            sale.setVipId(vipId);

            saleMapper.insert(sale);

        }

        status.setMsg("添加成功！");
        status.setCode(true);
        return status;
    }
    //对sale表查询所有

    public List<Sale> selectSale(){

        List<Sale> list = saleMapper.selectList(null);
//        list.forEach(System.out::println);
        return list;

    }
    //根据订单号删除
    public Status deleteSale(Integer saleId){
        Status status = new Status();
        if (saleId==null){
            status.setMsg("没有可选择的订单！");
            status.setCode(false);
            return status;
        }
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("sale_id",saleId);

        saleMapper.deleteByMap(hashMap);
        status.setMsg("删除成功！");
        status.setCode(true);
        return status;

    }
}
